package com.cimpm.project.service.impl;

import com.cimpm.project.entity.ProjectRole;
import com.hby.common.callback.AutoProcessControllerPart;
import com.hby.common.entities.aspect.parameter.MapperParam;
import com.hby.common.entities.aspect.parameter.ServiceInfo;
import com.hby.common.exceptions.AutoException;
import com.hby.common.exceptions.ExceptionEnum;
import com.hby.common.services.Impl.mian.AutoEntityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class ProjectRoleServiceImpl extends AutoEntityServiceImpl<ProjectRole> {

    @Autowired
    private ProjectServiceImpl projectService;
    @Autowired
    private ProjectroleMapperServiceImpl projectroleMapperService;

    @Override
    public void customService(AutoProcessControllerPart autoProcessControllerPart, ServiceInfo serviceInfo) throws Throwable {
        autoProcessControllerPart.setBeforeGetPartCallback(() -> {
            if (!serviceInfo.getParamConverter().containsKey("projectId"))
                throw new AutoException(ExceptionEnum.PARAM_FIELD, "需要projectID");
            Integer projectId = serviceInfo.getParamConverter().getInt("projectId");
            projectService.getEntityFromId(projectId, false);
            Set<Integer> roleIds = projectroleMapperService.getSecondaryIdsFromPrimaryId(projectId);
            Map<String, Object> filterMap = new HashMap<>();
            filterMap.put("id", roleIds);
            serviceInfo.getParamConverter().getParamMap().put("filterMap", filterMap);
            return null;
        });
        autoProcessControllerPart.setBeforeAddPartCallback(() -> {
            if (!serviceInfo.getParamConverter().getParamMap().containsKey("projectId"))
                throw new AutoException(ExceptionEnum.PARAM_FIELD, "缺少ProjectId参数");
            return null;
        });
        autoProcessControllerPart.setAfterAddPartCallback(() -> {
            Integer insertId = serviceInfo.getNewId();
            projectroleMapperService.addMapper(new MapperParam(serviceInfo.getParamConverter().getInt("projectId"), insertId));
            return null;
        });
    }
}
